package com.ziyia.uicustomviews;

import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.util.ArrayList;
import java.util.List;

public class android_196 extends AppCompatActivity {
    private EditText inputText;
    private Button send;
    private MsgAdapter msgAdapter;
    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_android196);
        init();

        List<Msg> msgList = new ArrayList<>();
        msgList.add(new Msg("老板，在吗？", Msg.TYPE_RECEIVED));
        msgList.add(new Msg("在的，有什么事吗？", Msg.TYPE_SENT));
        msgList.add(new Msg("嘿嘿，我这边要2台川崎H2，请尽快安排发货。", Msg.TYPE_RECEIVED));
        msgList.add(new Msg("OK，必须安排！！！", Msg.TYPE_SENT));
        msgList.add(new Msg("老板，你如何看待叙利亚局势？", Msg.TYPE_RECEIVED));
        msgList.add(new Msg("躺着看。", Msg.TYPE_SENT));


        recyclerView = findViewById(R.id.recycleView);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        msgAdapter = new MsgAdapter(this, msgList);
        recyclerView.setAdapter(msgAdapter);





        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String value = inputText.getText().toString();
                Log.d("接收的数据", value);
                if (!value.isEmpty()) {
                    Msg msg = new Msg(value, Msg.TYPE_SENT);
                    boolean add = msgList.add(msg);
                    if (msgAdapter != null) {
                        msgAdapter.notifyItemInserted(msgList.size() - 1);
                        recyclerView.scrollToPosition(msgList.size() - 1);
                        inputText.setText("");
                    }

                }
            }
        });
    }

    private void init() {
        send = findViewById(R.id.send);
        inputText = findViewById(R.id.inputText);
    }


}